package com.tledu.stu_manager.controller.student; /**
 *
 */

import com.tledu.stu_manager.dao.StudentDao;
import com.tledu.stu_manager.exception.CustomException;
import com.tledu.stu_manager.model.Student;
import com.tledu.stu_manager.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StuSearchServlet", value = "/stu/search")
public class StuSearchServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        super.init();
        studentService = new StudentService(new StudentDao());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String username = req.getParameter("username");
            List<Student> list = studentService.fuzzyQueryByUsername(username);
            req.setAttribute("stu_list", list);
            req.getRequestDispatcher("/WEB-INF/student/student-list.jsp").forward(req, resp);
        } catch (CustomException e) {
            e.printStackTrace();
            req.setAttribute("msg", e.getMessage());
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }
    }
}
